import json,io
import xlrd,arcpy, arcgisscripting, sys, os

gp = arcgisscripting.create()
gp.OverwriteOutput = 1
outputShpPath = sys.argv[1]
outWorkspace = os.path.split(outputShpPath)[0]
shpName = os.path.split(outputShpPath)[-1]
spat_ref = "4326"
gp.CreateFeatureClass_management(outWorkspace, shpName, "POLYGON", "", "", "", spat_ref)
gp.addfield(outputShpPath, "NAME", "text", "", "", "100")
cur = gp.InsertCursor(outputShpPath)
newRow = cur.newRow()

path ='E:\\PythonProject\\africa\\doc\\africa\\countries.geojson'
with open(path, 'r') as f:
    data = json.load(f)

for i in range(0, len(data["features"])):
    name = data["features"][i]["properties"]["name"]
    type = data["features"][i]["geometry"]["type"]
    coors = data["features"][i]["geometry"]["coordinates"]
    # if type=='Polygon' and len(coors)!=1:
    #     arcpy.AddMessage('row:{0},geometryType:{1},coorLength:{2}'.format(i, type, len(coors)))
    if type == 'Polygon':
        polygons = coors
        for j in range(0, len(polygons)):
            polygon = polygons[j]
            newRow.NAME = name
            array = gp.CreateObject("array")
            pnt = gp.CreateObject("point")
            for k in range(0, len(polygon)):
                pos = polygon[k]
                # arcpy.AddMessage('pos:{0}'.format(pos))
                pnt.X, pnt.Y = pos[0], pos[1]
                array.add(pnt)
            newRow.Shape = array
            cur.InsertRow(newRow)
    elif type == 'MultiPolygon':
        polygons = coors
        for j in range(0, len(polygons)):
            polygon = polygons[j]
            newRow.NAME = name
            array = gp.CreateObject("array")
            pnt = gp.CreateObject("point")
            for k in range(0, len(polygon)):
                pos = polygon[k]
                for l in range(0,len(pos)):
                    # arcpy.AddMessage('pos:{0}'.format(pos))
                    pnt.X, pnt.Y = pos[l][0], pos[l][1]
                    array.add(pnt)
                newRow.Shape = array
                cur.InsertRow(newRow)

del cur, newRow


